package com.iptv.grape.ui.frgmt;

import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.iptv.grape.R;
import com.iptv.grape.bu.AppConfig;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.event.EventArgs;
import com.iptv.grape.event.data.VodData;
import com.iptv.grape.ui.BaseFrgmt;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.util.CommUtil;
import com.iptv.grape.video.DefXMediaPlayerEventListener;
import com.iptv.grape.video.IPlayCompletion;
import com.iptv.grape.video.IProgressListener;
import com.iptv.grape.video.XMediaPlayer;

import org.greenrobot.eventbus.EventBus;

// 点播的碎片对象
public class VodFragment extends BaseFrgmt implements SeekBar.OnSeekBarChangeListener,
        IProgressListener, IPlayCompletion,
        View.OnFocusChangeListener {

    // Activity
    private SeekBar seekbar;
    //private TextView time_left;
    private TextView time_right;
    private TextView timing;
    private DefXMediaPlayerEventListener xMediaPlayerEventListener;
    @Override
    protected int layoutId() {
        return R.layout.vod_fragment;
    }

    @Override
    protected void initView(@NonNull View root, @Nullable Bundle savedInstanceState) {
        //time_left = root.findViewById(R.id.time_left);
        time_right = root.findViewById(R.id.time_right);
        timing = root.findViewById(R.id.timing);
        seekbar = root.findViewById(R.id.seekbar);
        seekbar.setTag("seekbar");
        seekbar.setOnSeekBarChangeListener(this);
        seekbar.setOnFocusChangeListener(this);

        xMediaPlayerEventListener = mActivity.getDefXMediaPlayerEventListener();
        xMediaPlayerEventListener.addProgressListener(this);
        xMediaPlayerEventListener.addPlayCompletionListener(this);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setSeekbar(seekbar);
        //GrapeContext.curGrapeContext().getCurMediaPlayer().setOnPlayerListener(this);



        this.time_right.setText(CommUtil.getShowTimeStringByEnd(GrapeContext.curGrapeContext().getCurMediaPlayer().getDuration()));
        seekbar.setMax(GrapeContext.curGrapeContext().getCurMediaPlayer().getDuration());
        seekbar.setKeyProgressIncrement(1000 * AppConfig.getAppConfig().getVodModeKeyProgressIncrement());
        seekbar.setProgress(GrapeContext.curGrapeContext().getCurMediaPlayer().getCurrentPosition());
        seekbar.requestFocus();
    }

    // 销毁
    public void onDestroy() {
        super.onDestroy();
        //GrapeContext.curGrapeContext().getCurMediaPlayer().cleanSeekbar();
        //GrapeContext.curGrapeContext().getCurMediaPlayer().removeListener(this);
        xMediaPlayerEventListener.removePlayCompletionListener(this);
        xMediaPlayerEventListener.removeProgressListener(this);
    }

    // 界面隐藏显示回调函数
    @Override
    public void onHiddenChanged(boolean hidden){
        super.onHiddenChanged(hidden);
        // 不在最前端界面显示
        if (hidden) {
            // 隐藏
        } else {
            this.time_right.setText(CommUtil.getShowTimeStringByEnd(GrapeContext.curGrapeContext().getCurMediaPlayer().getDuration()));
            seekbar.setMax(GrapeContext.curGrapeContext().getCurMediaPlayer().getDuration());
            seekbar.setKeyProgressIncrement(1000 * AppConfig.getAppConfig().getVodModeKeyProgressIncrement());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //rel_horitation.reSetFocus();
                    seekbar.requestFocus();

                }
            }, 100);
        }
    }

    public static VodFragment newInstance() {
        VodFragment frag = new VodFragment();
        Bundle bundle = new Bundle();
        //bundle.putString("dateTimeString", dateTimeString);
        frag.setArguments(bundle);   //设置参数
        return frag;
    }

    // 数据上报 Handler
    private Handler datalHandler = new Handler();

    // 数据上报 Runnable
    private Runnable vodDataHandlerRunnable = new Runnable() {

        @Override
        public void run() {
            VodData vodData = VodData.getCurVodData();
            if(vodData != null){
                vodData.setVodOperateType(lastVal);
                EventBus.getDefault().post(new EventArgs(AppConstant.Vod, AppConstant.DataAcquisition, vodData));
            }
        }
    };
    String lastVal;
    @Override
    public void onProgressChanged(SeekBar seekBar, int progressVal, boolean isUserSet) {
        if(isUserSet) {
            XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
            if(!mediaPlayer.isPlaying()){
                mediaPlayer.start();
            }
            lastVal = progressVal - mediaPlayer.getCurrentPosition() > 0 ? "1":"2";
            mediaPlayer.seekTo(progressVal);
            datalHandler.removeCallbacks(vodDataHandlerRunnable);
            //curChannel.playTimeMove(curTimeArga);
            datalHandler.postDelayed(vodDataHandlerRunnable,200);
        }
        // 进度条游标(HH:MM:SS)
        //timing.setText(CommUtil.formatDate(selectBill.getBillStartDate(),"HH:mm:ss"));
        int quotaWidth = timing.getWidth();
        final Drawable thumb = seekBar.getThumb();
        //获取thumb的位置
        final Rect bounds = thumb.getBounds();


        //curStartDate.setTime(curStartDate.getTime() + i);
        timing.setText(CommUtil.getShowTime(progressVal));
        timing.setX((thumb.getIntrinsicWidth() - quotaWidth) / 2
                + bounds.left + seekBar.getX());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        AppLog.getCurLog().out("onStartTrackingTouch");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        AppLog.getCurLog().out("onStopTrackingTouch");
        XMediaPlayer mediaPlayer = GrapeContext.curGrapeContext().getCurMediaPlayer();
        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
        }
        mediaPlayer.seekTo(seekBar.getProgress());
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            timing.setBackgroundResource(R.drawable.timing_fucs);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.white));
        }else {
            timing.setBackgroundResource(R.drawable.timing_blus);
            timing.setTextColor(this.mActivity.getResources().getColor(R.color.item_bill4));
        }
    }

    @Override
    public void onProgress(int speed) {
        //AppLog.getCurLog().out("VodFragment -------- onProgress" + speed);
        seekbar.setProgress(speed);
    }

    @Override
    public void playCompletion() {
        //AppLog.getCurLog().out("VodFragment -------- playCompletion");
    }
}
